//
//  ViewController.swift
//  ArchitectGenerics
//
//  Created by 姚胜龙 on 2017/6/5.
//  Copyright © 2017年 姚胜龙. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //泛型测试
        //let generics = GenericsTest1()
//        var num1 = 20, num2 = 100
//        generics.swipTowNumber(a: &num1, b: &num2)
//        print("交换之后的 num1 = \(num1) \n num2 = \(num2)")
//
//        var num1:Int = 20, num2:Int = 100
//        generics.swipTwoNum(a: &num1, b: &num2)
//
//        var num3:Double = 55.5, num4:Double = 110.5
//        generics.swipTwoNum(a: &num3, b: &num4)
//
//        print("交换之后的 num1 = \(num1)  num2 = \(num2) num3 = \(num3)  num4 = \(num4)")


        //类泛型设计测试,使用的时候再指定类型
//        let array = ArrayList<Int>()
//        array.add(obj: 100)
//        array.add(obj: 200)
//        array.add(obj: 300)
//        array.forEach()
//        let arrayDouble = ArrayList<Double>()
//        arrayDouble.add(obj: 100.5)
//        arrayDouble.add(obj: 200.5)
//        arrayDouble.add(obj: 300.5)
//        arrayDouble.forEach()
        //泛型继承测试
//        let array = YSLArrayList<Int>()
//        array.add(obj: 100)
//        array.add(obj: 200)
//        array.add(obj: 300)
//        array.forEach()
//        array.removeAll()

        //泛型协议编程
//        let array = InheritArrayList<Double>()
//        array.add(obj: 11.0)
//        array.add(obj: 21.0)
//        array.add(obj: 31.0)
//        array.forEach()
//        array.removeAll()

        //限制泛型协议
//        let userModel = UserModel()
//        userModel.password = "123456"
//        userModel.userName = "Administritor"
//        let modelArray = YSL_ModelArray<BaseModel>()
//        modelArray.add(obj: userModel)
//        modelArray.forEach()

        /*
        let alertVC = UIAlertController(title: "名称", message: "信息", preferredStyle: .alert)
        let view1 = alertVC.view.subviews[0]
        //print("view1 = \(view1)")
        let view2 = view1.subviews.first
        //print("view2 = \(view2!)")
        let view3 = view2?.subviews.first
        //print("view3 = \(view3!)")
        let view4 = view3?.subviews.first
        //print("view4 = \(view4!)")
        let view5 = view4?.subviews.first
        //print("view5 = \(view5!)")
        let title:UILabel = view5?.subviews.first as! UILabel
        let message:UILabel = view5?.subviews[1] as! UILabel
        title.textColor = UIColor.red
        print("title.text = \(title.text!)")
        print("message.text = \(message.text!)")

        alertVC.addAction(.init(title: "确定", style: UIAlertActionStyle.cancel, handler: nil))
        present(alertVC, animated: true) { 

        }
         */

        //命令模式测试一 电脑关机和开机
//        //1、创建接受者
//        let computerReciver = MacComputer()
//        //2、创建开机关机命令
//        let startupCommand = MacStartupCommand(macComputer: computerReciver)
//        let shutdownCommand = MacShutdownCommand(macComputer: computerReciver)
//
//        //3、创建请求者
//        let invoker = MacInvoker(startupCommand: startupCommand, shutdownCommand: shutdownCommand)
//        //4、执行命令
//        invoker.startup()
//        invoker.shutdown()

        //=============俄罗斯方块命令测试=================
//        //1、创建接收者
//        let tetrisMeshine = TetrisMeshine()
//        //2、创建命令
//        let left = TMLeftCommand(tm: tetrisMeshine)
//        let right = TMRightCommand(tm: tetrisMeshine)
//        let fastDown = TMFastDownCommand(tm: tetrisMeshine)
//        let transform = TMTransformCommand(tm: tetrisMeshine)
//        //3、创建请求者
//        let tmButton = TMButtons(leftCommand: left, rightCommand: right, fastDownCommand: fastDown, transformCommand: transform)
//        //客户端请求
//        tmButton.toLeft()
//        tmButton.toRight()
//        tmButton.fastDown()
//        tmButton.transform()

//        //==========支持撤销操作=======
//        //1、创建接收者
//        let tetrisMeshine = TetrisMeshine()
//        //2、创建命令
//        let left = TMLeftCommand(tm: tetrisMeshine)
//        let right = TMRightCommand(tm: tetrisMeshine)
//        let fastDown = TMFastDownCommand(tm: tetrisMeshine)
//        let transform = TMTransformCommand(tm: tetrisMeshine)
//        //3、创建请求者
//        let tmButton = TMButtons1(tetris:tetrisMeshine,leftCommand: left, rightCommand: right, fastDownCommand: fastDown, transformCommand: transform)
//        //客户端请求
//        tmButton.toLeft()
//        tmButton.toRight()
//        tmButton.fastDown()
//        tmButton.transform()
//        print("------------------------------------")
//        //撤销变形操作
//        tmButton.undoCommand()

//        //接受者
//        let tetrisMeshine = TetrisMeshine()
//        let button4 = TMButtons2(reciver: tetrisMeshine)
//        button4.toLeft()
//        button4.transform()
//        button4.undoCommand()

//        //测试：动态的指定具体的命令
//        let tetrisMeshine = TetrisMeshine()
//        let button = TMButtons3(reciver: tetrisMeshine)
//        button.toLeft()
//        button.transform()
//        button.undoCommand()

//        //TMButtons4 测试同步代码块，解决并发问题
//        let tetrisMeshine = TetrisMeshine()
//        let button = TMButtons4(reciver: tetrisMeshine)
//        button.toLeft()
//        button.transform()
//        button.undoCommand()

        //TMButtons5 测试闭包命令
//        let tetrisMeshine = TetrisMeshine()
//        let button = TMButtons5(reciver: tetrisMeshine)
//        button.toLeft()
//        button.transform()
//        button.undoCommand()

        //=======原型设计模式======================
        //===========浅度拷贝测试===========
        //原型对象
//        let user = UserCopyModel(name: "YSL", sex: "男")
//        //克隆对象
//        let copyUser:UserCopyModel = user.clone() as! UserCopyModel
//        //修改克隆对象的值，看看原型对象会不会改变
//        copyUser.name = "Andy"
//        copyUser.sex = "man"
//        print("原型对象:name = \(user.name!) sex= \(user.sex!)")
//        print("克隆对象:name = \(copyUser.name!) sex= \(copyUser.sex!)")

        //========浅拷贝的Array测试============
//        let array = ["Dream","Hank","CC"]
//        let student = StudentCopyModel(name: "Mimi", teachers: array)
//        let copyStudent: StudentCopyModel = student.clone() as! StudentCopyModel
//        copyStudent.name = "DaChanglian"
//        copyStudent.teachers = ["Virtl","James","Lisa"]
//        print("原型对象:name = \(student.name!) teachers = \(student.teachers!)")
//        print("克隆对象:name = \(copyStudent.name!) teachers = \(copyStudent.teachers!)")

        //--––––––––––––––––––––––––––––––––––––––––––––
        
//        let array = [TeacherModel(name:"Jack"),TeacherModel(name:"Rose")]
//        let student = StudentCopyModel(name: "Mimi", teachers: array)
//        let copyStudent: StudentCopyModel = student.clone() as! StudentCopyModel
//        copyStudent.name = "DaChanglian"
//        copyStudent.teachers?[0].name = "Lisa"
////        print("原型对象:name = \(student.name!) teachers = \(String(describing: student.teachers![0].name))")
////        print("克隆对象:name = \(copyStudent.name!) teachers = \(String(describing: copyStudent.teachers![0].name))")
//        print("原型对象：teachers = \(student.teachers!)\n克隆对象：teachers = \(copyStudent.teachers!)")


        //=========深度拷贝测试=================
//        let array = [TeacherModel2(name:"Jack"),TeacherModel2(name:"Rose")]
//        let student = DeepCopyStudentModel(name: "Mimi", teachers: array)
//        let copyStudent: DeepCopyStudentModel = student.deepClone() as! DeepCopyStudentModel
//        copyStudent.name = "DaChanglian"
//        copyStudent.teachers?[0].name = "Lisa"
//        print("原型对象:name = \(student.name!) teachers = \(String(describing: student.teachers![0].name))")
//        print("克隆对象:name = \(copyStudent.name!) teachers = \(String(describing: copyStudent.teachers![0].name))")

        //=========系统的NSCopying协议============
//        let array = [TeacherModel3(name:"Jack"),TeacherModel3(name:"Rose")]
//        let student = StudentModel3(name: "Mimi", teachers: array)
//        let copyStudent: StudentModel3 = student.copy() as! StudentModel3
//        copyStudent.name = "DaChanglian"
//        copyStudent.teachers?[0].name = "Lisa"
//        print("原型对象:name = \(student.name!) teachers = \(String(describing: student.teachers![0].name))")
//        print("克隆对象:name = \(copyStudent.name!) teachers = \(String(describing: copyStudent.teachers![0].name))")

//        ++++++++++++++++++ 享元模式++++++++++++++++++++++++++

//        //测试1
//        let train = YSL_FlyweightTicketFactory.ticket(from: "北京", to: "上海")
//        train.showTicketInfo(bunk: "一等座")
//
//        for _ in 0...100000 {
//            let train = FlyweightTicketFactory.ticket(from: "北京", to: "上海")
//            train.showTicketInfo(bunk: "一等座")
//        }
//        ================语法=====================
//        SimpleTest.arrayTest()
        //SimpleTest.setTest()



//========================== 访问者模式=========
//        let report = EmployeeReport()
//        report.showReport(visitor: SL_CTOVisitor(name: "小明"))
//
//        report.showReport(visitor: SL_CEOVisitor(name: "小红"))

//       -------- 访问者模式垃圾代码测试 ------------
//        let shape = ShapeCollection()
//        let length = shape.caculateTotalLength()
//        print("总长度：\(length)")
//
//        let totalArea = shape.caculateAreas()
//        print("总面积:\(totalArea)")

//       -------- 访问者模式优化代码测试 ------------
        let shapeCollection = ShapeCollection1()
        //计算长度访问者
        let lengthVisitor = CalculateShapeLengthVisitor()
        shapeCollection.accept(visitor: lengthVisitor)
        print("总长度：\(lengthVisitor.totalLength)")

//        计算面积访问者
        let  areaVisitor = CalculateAreaVisitor()
        shapeCollection.accept(visitor: areaVisitor)
        print("总面积：\(areaVisitor.totalArea)")
        
    }


    //UIAlertController 是属于Builder设计模式
    @IBOutlet weak var showMeaasge: UIButton!
    @IBAction func showMessageAlert(_ sender: UIButton) {
        //    ---------普通的消息弹框的写法--start-------------------
//        let alert = UIAlertController(title: "提示", message: "你先确定要删除吗？", preferredStyle: UIAlertControllerStyle.alert)
//        alert.addAction(UIAlertAction(title: "确定", style: UIAlertActionStyle.destructive, handler: { (UIAlertAction) in
//            print("点击了确定");
//        }))
//
//        alert.addAction(UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel, handler: { (UIAlertAction) in
//            print("点击了取消");
//        }))
//        self.present(alert, animated: true) { 
//
//        }
        //    ---------普通的消息弹框的写法--end-------------------

        //构建者设计的alert使用写方法
        let alert = SLAlertController.Builder(context: self)
        alert.setTitle("提示").setMessage("你确定要删除这张图片吗？").setCancelTitle("取消").setCancelHandle { (UIAlertAction) in
            print("取消")
        }.setConfirmTitle("确定").setConfirmHandle { (UIAlertAction) in
            print("确定")
        }.build().showAlert()

    }


    //撤销操作
    @IBOutlet weak var textFeild: UITextField!

    @IBAction func undo(_ sender: Any) {
        undoManager?.undo()
    }

    @IBAction func saveLogs(_ sender: Any) {
        undoManager?.registerUndo(withTarget: self, selector: #selector(ViewController.undoContent), object: self.textFeild.text)
    }

    func undoContent(text:String?) {
        self.textFeild.text = text
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

